package com.shakev.coupon.web.controller;

import com.shakev.coupon.business.model.GoodsDetailDto;
import com.shakev.coupon.es.model.BrowseRecord;
import com.shakev.coupon.es.model.GoodsCond;
import com.shakev.coupon.es.model.Mark;
import com.shakev.coupon.es.service.EBrowseRecordService;
import com.shakev.coupon.es.service.EGoodsDetailService;
import com.shakev.coupon.es.service.EMarkService;
import com.shakev.coupon.es.service.impl.TkService;
import com.shakev.coupon.taobao.model.GoodsDetail;
import com.shakev.coupon.web.model.base.RestResponse;
import com.taobao.api.ApiException;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @ClassName GoodsDetailController
 * @Description TODO
 * @Author 李伟松
 * @CreateDate 2019-05-09 下午 10:34
 * @UpdateDate 2019-05-09 下午 10:34
 * @Version 1.0
 */
@RestController
public class GoodsDetailController {

    @Resource
    TkService tkService;

    @Resource
    EGoodsDetailService goodsDetailService;

    @Resource
    EBrowseRecordService browseRecordService;

    @Resource
    EMarkService markService;

    @RequestMapping("/goods/detail")
    public RestResponse goodsDetail(Long id){

        //添加浏览记录
        BrowseRecord browseRecord = new BrowseRecord();
        browseRecord.setUserId("1");
        browseRecord.setItemId(id.toString());
        BrowseRecord res = browseRecordService.findByBrowseRecord(browseRecord);
        if (res == null) {
            BrowseRecord result = browseRecordService.save(browseRecord);
        }


        GoodsDetail detail = goodsDetailService.findById(id);
        if(detail!=null){
            GoodsDetailDto detailDto = new GoodsDetailDto();

            //判断是否被收藏
            Mark mark = new Mark();
            mark.setItemId(id.toString());
            mark.setUserId("1");
            Mark isMark = markService.findByMark(mark);
            if(isMark!=null){
                detail.setMarkState(1);
            }else{
                detail.setMarkState(0);
            }
            return RestResponse.success(detail);
        }

        return RestResponse.success();

    }

    @RequestMapping("/goodsByCid")
    public RestResponse goodsByCid(Integer pageNo,Integer pageSize ,Long cid){
        GoodsCond gc = new GoodsCond();
        gc.setCid(cid);
        Page<GoodsDetail> list = goodsDetailService.pageQueryByCid(pageNo, pageSize, gc);
        if(list!=null ){
            return RestResponse.success(list.getContent());
        }else{
            return RestResponse.success();
        }

    }

    @RequestMapping("/goodsByOneCid")
    public RestResponse goodsByOneCid(Integer pageNo,Integer pageSize ,Long cid){
        GoodsCond gc = new GoodsCond();
        gc.setCid(cid);
        Page<GoodsDetail> list = goodsDetailService.pageQueryByOneCid(pageNo, pageSize, gc);
        if(list!=null ){
            return RestResponse.success(list.getContent());
        }else{
            return RestResponse.success();
        }

    }

    @RequestMapping("/goods/add")
    public RestResponse addGoodsDetail(Long materialId,Long pageNo,Long pageSize){

        materialId = (materialId==null?3767L:materialId);
        try {
            tkService.exec(materialId,pageNo,pageSize);
        } catch (ApiException e) {
            e.printStackTrace();
        }
//        3756L
        return RestResponse.success();
    }

    @RequestMapping("/goods/search")
    public RestResponse search(String keyworks,Integer pageNo,Integer pageSize){
        Page<GoodsDetail> list = goodsDetailService.search(pageNo, pageSize, keyworks);
        if(list!=null ){
            return RestResponse.success(list.getContent());
        }else{
            return RestResponse.success();
        }
    }
}
